package com.zx1316.javasimpledoudizhu.client.ui;

import com.zx1316.javasimpledoudizhu.Main;
import com.zx1316.javasimpledoudizhu.misc.Card;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.IOException;
import java.util.Vector;

public class UIHelper {
    private final ImageView[][] discardArray;
    private final ImageView[] hand;
    private final Label[] tips;
    private final Label[] names;
    private final Label[] scores;
    private final Label[] landlords;
    private final Label[] remainings;
    private final Label[] doubles;
    private final Label[] infos;

    public UIHelper(ClientController controller) {
        ImageView[] p1Discards = new ImageView[]{
                controller.p1Discard1, controller.p1Discard2, controller.p1Discard3, controller.p1Discard4, controller.p1Discard5,
                controller.p1Discard6, controller.p1Discard7, controller.p1Discard8, controller.p1Discard9, controller.p1Discard10,
                controller.p1Discard11, controller.p1Discard12, controller.p1Discard13, controller.p1Discard14, controller.p1Discard15,
                controller.p1Discard16, controller.p1Discard17, controller.p1Discard18, controller.p1Discard19, controller.p1Discard20};
        ImageView[] p2Discards = new ImageView[]{
                controller.p2Discard1, controller.p2Discard2, controller.p2Discard3, controller.p2Discard4, controller.p2Discard5,
                controller.p2Discard6, controller.p2Discard7, controller.p2Discard8, controller.p2Discard9, controller.p2Discard10,
                controller.p2Discard11, controller.p2Discard12, controller.p2Discard13, controller.p2Discard14, controller.p2Discard15,
                controller.p2Discard16, controller.p2Discard17, controller.p2Discard18, controller.p2Discard19, controller.p2Discard20};
        ImageView[] p3Discards = new ImageView[]{
                controller.p3Discard1, controller.p3Discard2, controller.p3Discard3, controller.p3Discard4, controller.p3Discard5,
                controller.p3Discard6, controller.p3Discard7, controller.p3Discard8, controller.p3Discard9, controller.p3Discard10,
                controller.p3Discard11, controller.p3Discard12, controller.p3Discard13, controller.p3Discard14, controller.p3Discard15,
                controller.p3Discard16, controller.p3Discard17, controller.p3Discard18, controller.p3Discard19, controller.p3Discard20};
        this.discardArray = new ImageView[][]{p1Discards, p2Discards, p3Discards};
        this.hand = new ImageView[]{
                controller.card1, controller.card2, controller.card3, controller.card4, controller.card5, controller.card6,
                controller.card7, controller.card8, controller.card9, controller.card10, controller.card11, controller.card12,
                controller.card13, controller.card14, controller.card15, controller.card16, controller.card17, controller.card18,
                controller.card19, controller.card20};
        this.tips = new Label[]{
                controller.oJB, controller.oJS, controller.o2, controller.oA, controller.oK, controller.oQ, controller.oJ,
                controller.o10, controller.o9, controller.o8, controller.o7, controller.o6, controller.o5, controller.o4,
                controller.o3};
        this.names = new Label[]{controller.name1, controller.name2, controller.name3};
        this.scores = new Label[]{controller.score1, controller.score2, controller.score3};
        this.landlords = new Label[]{controller.landlord1, controller.landlord2, controller.landlord3};
        this.remainings = new Label[]{controller.remaining1, controller.remaining2, controller.remaining3};
        this.doubles = new Label[]{controller.double1, controller.double2, controller.double3};
        this.infos = new Label[]{controller.info1, controller.info2, controller.info3};
    }

    public void clearDiscards(int id) {
        for (int i = 0; i < 20; i++) {
            discardArray[id][i].setImage(null);
        }
    }

    public void clearHands() {
        for (int i = 0; i < 20; i++) {
            hand[i].setImage(null);
        }
    }

    public void clearNames() {
        for (int i = 0; i < 3; i++) {
            names[i].setText(" ");
        }
    }

    public void clearScores() {
        for (int i = 0; i < 3; i++) {
            scores[i].setText("0");
        }
    }

    public void clearLandlords() {
        for (int i = 0; i < 3; i++) {
            landlords[i].setVisible(false);
        }
    }

    public void clearRemainings() {
        for (int i = 0; i < 3; i++) {
            remainings[i].setText("0");
        }
    }

    public void resetRemainings() {
        for (int i = 0; i < 3; i++) {
            remainings[i].setText("17");
        }
    }

    public void clearDoubles() {
        for (int i = 0; i < 3; i++) {
            doubles[i].setVisible(false);
        }
    }

    public void clearInfos() {
        for (int i = 0; i < 3; i++) {
            infos[i].setText("");
        }
    }

    public void setInfo(int id, String str) {
        infos[id].setText(str);
    }

    public void tipsDec(Card card) {
        tips[card.getRelative()].setText(String.valueOf(Integer.parseInt(tips[card.getRelative()].getText()) - 1));
    }

    public void resetTips() {
        tips[0].setText("1");
        tips[1].setText("1");
        for (int i = 2; i < 15; i++) {
            tips[i].setText("4");
        }
    }

    public void showHands(Vector<Card> cardsList) throws IOException {
        int startPos = (20 - cardsList.size()) / 2;
        int pos;
        for (pos = 0; pos < startPos; pos++) {
            hand[pos].setVisible(false);
        }
        for (Card card : cardsList) {
            hand[pos].setVisible(true);
            hand[pos].setImage(new Image(Main.class.getResource(card.content() + ".png").openStream()));
            pos++;
        }
        for (; pos < 20; pos++) {
            hand[pos].setVisible(false);
        }
    }

    public void showDiscards(Vector<Card> discards, int id) throws IOException {
        infos[id].setText("");
        if (id == 0) {
            for (int i = 0, start = (20 - discards.size()) / 2, len = discards.size(); i < len; i++) {
                discardArray[0][i + start].setImage(new Image(Main.class.getResource(discards.get(i).content() + ".png").openStream()));
            }
        } else {
            for (int i = 0, len = discards.size(); i < len; i++) {
                discardArray[id][i].setImage(new Image(Main.class.getResource(discards.get(i).content() + ".png").openStream()));
            }
        }
        remainings[id].setText(String.valueOf(Integer.parseInt(remainings[id].getText()) - discards.size()));
    }

    public void showDiscards(String[] msg, int id) throws IOException {
        infos[id].setText("");
        for (int i = 0, len = msg.length - 2; i < len; i++) {
            discardArray[id][i].setImage(new Image(Main.class.getResource(msg[i + 1] + ".png").openStream()));
        }
    }

    public void showScoreChange(int id, String score) {
        int tmp = Integer.parseInt(score) - Integer.parseInt(scores[id].getText());
        String tmpStr;
        if (tmp > 0) {
            tmpStr = "+" + tmp;
        } else {
            tmpStr = String.valueOf(tmp);
        }
        scores[id].setText(scores[id].getText() + tmpStr);
    }

    public void updateScore() {
        for (int i = 0; i < 3; i++) {
            String str = scores[i].getText();
            String[] split1 = str.split("\\+");
            if (split1.length == 2) {
                scores[i].setText(String.valueOf(Integer.parseInt(split1[0]) + Integer.parseInt(split1[1])));
            } else {
                String[] split2 = str.split("-");
                if (split2.length == 2 && !"".equals(split2[0])) {
                    scores[i].setText(String.valueOf(Integer.parseInt(split2[0]) - Integer.parseInt(split2[1])));
                }
            }
        }
    }

    public int getPosFromName(String name) {
        int i = 0;
        while (!name.equals(names[i].getText())) {
            i++;
        }
        return i;
    }

    public void setLandlord(int id) {
        landlords[id].setVisible(true);
        remainings[id].setText("20");
    }

    public void setDouble(int id) {
        doubles[id].setVisible(true);
    }

    public void setScore(String score, int id) {
        scores[id].setText(score);
    }

    public void resetAll() {
        resetTips();
        clearInfos();
        clearNames();
        clearLandlords();
        clearDoubles();
        clearScores();
        clearRemainings();
        clearDiscards(0);
        clearDiscards(1);
        clearDiscards(2);
        clearHands();
    }
}
